package com.e.takeaway.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.e.takeaway.DetailActivity;
import com.e.takeaway.R;
import com.e.takeaway.po.Food;
import com.e.takeaway.po.MerChant;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.List;

public class FoodAdapter extends BaseAdapter {

    private List<Food> foods;
    private Context ctr;
    private LayoutInflater inflater;

    public FoodAdapter(Context context,List<Food> list){
        ctr = context;
        this.foods = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return foods.size();
    }

    @Override
    public Object getItem(int i) {
        return foods.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final Integer[] x = {0};
        Food food = foods.get(i);
        FoodAdapter.ViewHolder viewHolder = null;
        if (view == null){
            view = inflater.inflate(R.layout.item_detail,viewGroup,false);
            viewHolder = new  ViewHolder();
            viewHolder.food_img = view.findViewById(R.id.detail_mer_img);
            viewHolder.food_name = view.findViewById(R.id.detail_name);
            viewHolder.food_price = view.findViewById(R.id.detail_price);
            viewHolder.food_add = view.findViewById(R.id.detail_add);
            viewHolder.food_number = view.findViewById(R.id.detail_number);
            viewHolder.food_reduce = view.findViewById(R.id.detail_reduce);
            view.setTag(viewHolder);

        }else{
            viewHolder = (FoodAdapter.ViewHolder) view.getTag();
        }

        viewHolder.food_name.setText(food.getFoodName());
        viewHolder.food_price.setText(""+food.getFoodPrice());
        Picasso.with(ctr)
                .load(food.getFoodImg())
                .placeholder(viewHolder.food_img.getDrawable())
                .error(R.drawable.lunbo1)
                .into(viewHolder.food_img);

        ViewHolder finalViewHolder = viewHolder;
        viewHolder.food_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                x[0] = x[0] +1;
                finalViewHolder.food_reduce.setVisibility(View.VISIBLE);
                finalViewHolder.food_number.setVisibility(View.VISIBLE);
                finalViewHolder.food_number.setText(x[0].toString());
                Message message = new Message();
                message.what = 100;
                Bundle bundle = new Bundle();
                food.setNumber(x[0]);
                bundle.putSerializable("food",food);
                message.setData(bundle);
                DetailActivity.handler.sendMessage(message);
            }
        });
        viewHolder.food_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                x[0] = x[0]-1;
                if (x[0]<=0){
                    finalViewHolder.food_reduce.setVisibility(View.INVISIBLE);
                    finalViewHolder.food_number.setVisibility(View.INVISIBLE);
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putInt("food_id",food.getFoodId());
                    message.what = 200;
                    message.setData(bundle);
                    DetailActivity.handler.sendMessage(message);
                }else {
                    finalViewHolder.food_number.setText(x[0].toString());
                    Message message = new Message();
                    food.setNumber(x[0]);
                    Bundle bundle = new Bundle();
                    bundle.putInt("food_id",food.getFoodId());
                    message.what = 300;
                    message.setData(bundle);
                    DetailActivity.handler.sendMessage(message);
                }
            }
        });
        return view;

    }

    static class ViewHolder{
       public ImageView food_img;
       public TextView food_name;
       public TextView food_price;
       public ImageView food_add;
       public TextView food_number;
       public ImageView food_reduce;
    }
}
